package com.security.door.controller.persionManage;

import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.crPersioninfo;
import com.security.door.model.dto.crPersioninfoDto;
import com.security.door.service.IcrPersioninfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


@RestController
@Api(value = "WEB - crPersioninfoController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CrPersioninfoController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IcrPersioninfoService icrPersioninfoService;

    @GetMapping("/persion/queryListWithPage")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper<PageInfo<crPersioninfo>> queryListWithPage() {
        crPersioninfoDto entityDto = new crPersioninfoDto();
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        entityDto.setPageIndex(pageIndex / pageSize + 1);
        entityDto.setPageSize(pageSize);
        var list = icrPersioninfoService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @PostMapping(value = "/persion/Add")
    @ApiOperation(value = "添加人事资料", notes = "添加人事资料")
    public Wrapper insert(@RequestBody crPersioninfo entity) {
        entity.setCreatetime(new Date());
        if (null == entity.getGuid() || entity.getGuid().isBlank()) {
            if (icrPersioninfoService.hasExists(entity.getPersioncode()))
                return WrapMapper.error("编码已经存在");
            entity.setGuid(UUID.randomUUID().toString());
            icrPersioninfoService.save(entity);
        } else {
            icrPersioninfoService.update(entity);
        }
        return WrapMapper.ok("操作成功");
    }

    @GetMapping(value = "/persion/del/{guids}")
    @ApiOperation(value = "批量删除人员", notes = "批量删除人员")
    public Wrapper deleteMore(@PathVariable("guids") String guids) {
        if (icrPersioninfoService.deleteByIds(guids) > 0)
            return WrapMapper.ok("操作成功");
        else
            return WrapMapper.error("操作失败");
    }

    @GetMapping(value = "/persion/selectByPersionCode/{persionCode}")
    @ApiOperation(value = "根据人员编码查询人员信息", notes = "根据人员编码查询人员信息")
    public Wrapper selectByPersionCode(@PathVariable("persionCode") String persionCode) {
        var entity = icrPersioninfoService.selectByAreaCode(persionCode);
        return WrapMapper.ok(entity);
    }

    @GetMapping(value = "/persion/selectByAttenGroup/{attenGroup}")
    @ApiOperation(value = "根据排班编码查询人员", notes = "根据排班编码查询人员")
    public Wrapper selectByAttenGroup(@PathVariable("attenGroup") String attenGroup) {
        var entity = icrPersioninfoService.selectByAttenGroup(attenGroup);
        return WrapMapper.ok(entity);
    }

    @GetMapping(value = "/persion/selectAll")
    @ApiOperation(value = "根据所有人员", notes = "根据所有人员")
    public Wrapper selectAll() {
        var list = icrPersioninfoService.selectAll();
        return WrapMapper.ok(list);
    }

    @PostMapping(value = "/persion/updateAttend")
    @ApiOperation(value = "根据所有人员", notes = "根据所有人员")
    public Wrapper updateAll(@RequestBody crPersioninfo entity) {
        var list = icrPersioninfoService.updateAttenGroup(entity.getAttengroup(), entity.getPersioncode());
        return WrapMapper.ok(list);
    }
}